package com.kaiwukj.android.ufamily.mvp.ui.adapter;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.mcas.http.imageloader.glide.GlideArms;
import com.kaiwukj.android.ufamily.R;
import com.luck.picture.lib.entity.LocalMedia;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.ArrayList;

/**
 * @author Haife Android Developer
 * @company KW | 开物
 * @since 2019-07-21
 * 发帖子选取图片控件
 */
public class PostCardPickImageAdapter extends BaseQuickAdapter<LocalMedia, BaseViewHolder> {

    public PostCardPickImageAdapter() {
        super(R.layout.recycle_item_post_card_pick_image, new ArrayList<>());
        addChildClickViewIds(R.id.iv_delete);
    }

    public int getDataCount() {
        return getItemCount() - getFooterLayoutCount();
    }

    public boolean hasVideo() {
        for (LocalMedia media : getData()) {
            if (media.getMimeType().startsWith("video/")) {
                return true;
            }
        }
        return false;
    }

    public boolean isImage() {
        if (getData().size() > 0) {
            LocalMedia item = getItem(0);
            if (item != null) return item.getMimeType().startsWith("image/");
        }
        return false;
    }

    public boolean isVideo() {
        if (getData().size() > 0) {
            LocalMedia item = getItem(0);
            if (item != null) return item.getMimeType().startsWith("video/");
        }
        return false;
    }

    @Override
    protected void convert(BaseViewHolder helper, LocalMedia item) {

        if (item.getPath() != null) {
            QMUIRadiusImageView imageView = helper.getView(R.id.iv_post_card_pick_image);
            ImageView videoIv = helper.getView(R.id.iv_play_video);
            if (item.getMimeType().equals("video/mp4")) {
                videoIv.setVisibility(View.VISIBLE);
            } else {
                videoIv.setVisibility(View.GONE);
            }
            GlideArms.with(getContext()).load(item.getRealPath()).centerCrop().into(imageView);

        }

    }
}
